﻿<head> 
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
 <?php include('../connections/connect.php'); ?>
<?php
	/*----------------------------------------------------------------------------------------- ส่วนของ function chart ทั่วไป---------------------------------------------------------------------------------------------------------------*/
	/*---------------------------------------------- แปลงเมตรเป็นกิโลเมตร -------------------------------------------------------------------------*/	
	function changetokilo($height){
    
		$strtest=$height;
		if(strlen($strtest)<3){
			if(strlen($strtest)==3){
				$test = "." .substr($strtest,-3);
			}else if(strlen($strtest)==2){
				$test = ".0" .substr($strtest,-2);
			}else if(strlen($strtest)==1){
				$test = ".00" .substr($strtest,-1);
			}
			
		}else{
			$test = "." .substr($strtest,-3);
		}
		if(strlen($strtest)<3){
			$test2 = "0";
		}else{
			$test2 = substr($strtest,0,-3);
		}
			
		$sumsrt = $test2 . $test ;
		return $sumsrt;
	}
	/*---------------------------------------------- แปลงพศ. เป็นจำนวนปี -------------------------------------------------------------------------*/	
	function getage($year){
	
		$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
		$thisyear = date("Y", $tomorrow);
		
		if($year==0){
			$age = "ไม่ระบุอายุ" ;
		}
		else{
			$age = ($thisyear+543) - $year;
		}
		return $age;
	
	}
	
	/*-------------------------------------------------------- Get Subtitle ------------------------------*/
	function getSubtitle($type,$subType,$data,$connect){
		if($type==0){
			$subtitle = "สำนักงานประปาสาขาทั้งหมด";
		}else{
			if($type!='null' && $subType=='null' && $data=='null'){
				$str_type = "branch_id ='".$type."'";
			}else if($type!='null' && $subType=='' && $data=='null'){
				$str_type = "branch_id ='".$type."'";
			}else if($type!='null' && $subType!='null' && $data==''){
				$str_type = "branch_id ='".$type."'";
				if($subType!='null' && $subType!=''){
					$str_subType = " and zone_id='".$subType."'";
				}
			}else{
				$str_type = "branch_id ='".$type."'";
				$str_subType = " and zone_id='".$subType."'";
				$str_data = " and DMA_id='".$data."'";
			}
			$str="";
			$sqlcommand="select * from mwa_area where $str_type $str_subType $str_data group by branch_id, zone_id";
			$result = mysql_query($sqlcommand, $connect)or die(mysql_error()); 
			$num = mysql_num_rows($result);
			$row = mysql_fetch_assoc($result);
			
			if($type!='null' && $subType=='null' && $data=='null'){
				$subtitle = "สำนักงานประปา";
				$subtitle .= $row['branch_name']." ";
			}else if($type!='null' && $subType=='' && $data=='null'){
				$subtitle = "สำนักงานประปา";
				$subtitle .= $row['branch_name']." ";
			}else if($type!='null' && $subType!='null' && $data==''){
				$subtitle = "สำนักงานประปา";
				$subtitle .= $row['branch_name']." ";
				if($subType!='null' && $subType!=''){
					$subtitle .= $row['zone_name']." ";
				}
			}else{
				$subtitle = "สำนักงานประปา";
				$subtitle .= $row['branch_name']." ".$row['zone_name']." ".$row['DMA_name'];
			}
		}
		return $subtitle;
	}
 /*------------------------------------------------------------------------------------------------------------------------------*/ 
 
	
	
	/*----------------------------------------------------------------------------------------- ส่วนของ function drilldrown chart -------------------------------------------------*/
	/*---------------------------------------------- ใส่รหัสสาขาแล้ว ส่งข้อมูลออกมาเป็นชื่อสาขา -------------------------------------------------------------------------*/	
	function getBrachName($firstname){
	
		if($firstname=='01'){
			$branchname = "ภาค4 สสบ.";
		}else if($firstname=='02'){
			$branchname = "ภาค4 สสต.";
		}else if($firstname=='03'){
			$branchname = "ภาค2 สสญ.";
		}else if($firstname=='04'){
			$branchname = "ภาค3 สสน.";
		}else if($firstname=='05'){
			$branchname = "ภาค2 สสท.";
		}else if($firstname=='06'){
			$branchname = "ภาค2 สสม.";
		}else if($firstname=='07'){
			$branchname = "ภาค1 สสส.";
		}else if($firstname=='11'){
			$branchname = "ภาค4 สสภ.";
		}else if($firstname=='12'){
			$branchname = "ภาค2 สสล.";
		}else if($firstname=='13'){
			$branchname = "ภาค1สสพ.";
		}else if($firstname=='14'){
			$branchname = "ภาค4 สสว.";
		}else if($firstname=='15'){
			$branchname = "ภาค3 สสช.";
		}else if($firstname=='16'){
			$branchname = "ภาค3 สสข.";
		}else if($firstname=='17'){
			$branchname = "ภาค1 สสป.";
		}else if($firstname=='53'){
			$branchname = "ภาค3 สสมบ.";
		}else if($firstname=='54'){
			$branchname = "ภาค4 สสบท.";
		}else if($firstname=='55'){
			$branchname = "ภาค3 สสสภ.";
		}else if($firstname=='56'){
			$branchname = "ภาค4 สสมส.";
		}
		return $branchname;
	}
	/*--------------------------------------------------------------------------------------------------------------------------------------------*/
	/*---------------------------------------------------------------------------------------  เรียงลำดับสาขาจาก รหัสสาขา เป็นเรียงตามภาค -------------------------------------------------------------------------*/	
	function sortBranch($variable_array){
		
		$sorting = array();
		$sorting[0] = $variable_array[13];
		$sorting[1] = $variable_array[9];
		$sorting[2] = $variable_array[6];
		$sorting[3] = $variable_array[2];
		$sorting[4] = $variable_array[4];
		$sorting[5] = $variable_array[5];
		$sorting[6] = $variable_array[8];
		$sorting[7] = $variable_array[12];
		$sorting[8] = $variable_array[11];
		$sorting[9] = $variable_array[3];
		$sorting[10] = $variable_array[14];
		$sorting[11] = $variable_array[16];
		$sorting[12] = $variable_array[1];
		$sorting[13] = $variable_array[0];
		$sorting[14] = $variable_array[15];
		$sorting[15] = $variable_array[7];
		$sorting[16] = $variable_array[17];
		$sorting[17] = $variable_array[10];
		
		return $sorting;
	}
	/*--------------------------------------------------------------------------------------------------------------------------------------------*/
	/*-------------------------------ตรียมข้อมูล categories data ของสาขาเพื่อใส่ใน graph------------------------------*/
	function setCategoriesBranch($allbranchname,$countbranch){
	
		for ($i = 0;$i<=$countbranch;$i++){ 
			if($i<$countbranch){
			$categories_branch .= "'".$allbranchname[$i]."',";
			}else{
			$categories_branch .= "'".$allbranchname[$i]."'";
			}
		}
		return $categories_branch;
	}
	/*--------------------------------------------------------------------------------------------------------------------------------------------*/
	/*-----------------------------------------------------------------  set ข้อมูลเตรียมเอาลงกราฟ ---------------------------------------------------------------------------*/
	function printData($allbranchid,$allbranchname,$allbranchamt,$countbranch,$branch_zone_id,$zoneamt,$countzone,$branch_zone_dma_id,$dma_amt,$countdma){
	
		/*-------------------------------ตรียมข้อมูล categories data for branch------------------------------*/
		for ($i = 0;$i<=$countbranch;$i++){ 
			
			if($i<$countbranch){
			$categories_branch .= "'".$allbranchname[$i]."',";
			}else{
			$categories_branch .= "'".$allbranchname[$i]."'";
			}
		}
		
		/*-------------------------------ตรียมข้อมูล categories data for zone------------------------------*/
		$categories_zone = array(); 
		for($i = 0;$i<=$countbranch;$i++){
			for($j =0;$j<=$countzone[$i];$j++){

				if($j<$countzone[$i]){
					$categories_zone[$i] .= "'zone".$branch_zone_id[$i][$j]."',";
				}else{
					$categories_zone[$i] .= "'zone".$branch_zone_id[$i][$j]."'";
				}
			}
		}
		
		$data_zone = array(); // set data for zone by branch
		for($i = 0;$i<=$countbranch;$i++){
			for($j =0;$j<=$countzone[$i];$j++){

				if($j<$countzone[$i]){
					$data_zone[$i] .= "".$zoneamt[$i][$j].",";
				}else{
					$data_zone[$i] .= "".$zoneamt[$i][$j]."";
				}
			}
		}
				
		/*-------------------------------ตรียมข้อมูล categories data for dma------------------------------*/
		$categories_dma = array(array());
		for ($i = 0;$i<=$countbranch;$i++){	
			for($j =0;$j<=$countzone[$i];$j++){
				for($k =0;$k<=$countdma[$i][$j];$k++){
					
					if($k<$countdma[$i][$j]){
						$categories_dma[$i][$j] .= "'DMA".$branch_zone_dma_id[$i][$j][$k]."',";
					}else{
						$categories_dma[$i][$j] .= "'DMA".$branch_zone_dma_id[$i][$j][$k]."'";
					}
					
				}
			}
		}
		
		$data_dma = array(array());	// set data for DMA
		for ($i = 0;$i<=$countbranch;$i++){	
			for($j =0;$j<=$countzone[$i];$j++){
				for($k =0;$k<=$countdma[$i][$j];$k++){
					
					if($k<$countdma[$i][$j]){
						$data_dma[$i][$j] .= "".$dma_amt[$i][$j][$k].",";
					}else{
						$data_dma[$i][$j] .= "".$dma_amt[$i][$j][$k]."";
					}
					
				}
			}
		}
		
		for($i = 0;$i<=$countbranch;$i++){
			for($j =0;$j<=$countzone[$i];$j++){
				$categories_dma[$i][$j];	// categories dma output 
				'<br>';
			}
		}
		for($i = 0;$i<=$countbranch;$i++){
			for($j =0;$j<=$countzone[$i];$j++){
				$data_dma[$i][$j];	// data dma output 
				'<br>';
			}
		}
		/*---------------------------------------------------------------------print ช้อมูลใส่ตัวแปร เพื่อเอาลงกราฟ ----------------------------------------------------------------------------------------*/
		
		$datalevel2 = array();
		$outputdatalevel2 = array();
		for($i = 0;$i<=$countbranch;$i++){
			for($j =0;$j<=$countzone[$i];$j++){
				$datalevel2[$i] = "{
									y: ".$zoneamt[$i][$j].",
									drilldown: {
										level: 2,
										name: 'zone".$branch_zone_id[$i][$j]."',
										categories: [".$categories_dma[$i][$j]."],
										data: [".$data_dma[$i][$j]."],
										color: colors[".$i."]
									}
								}";
				
				$outputdatalevel2[$i] .= $datalevel2[$i];
				if($countzone[$i]-$j>0){
					$outputdatalevel2[$i] .= ",";
				}
			}
		}
		
		for($i = 0;$i<=$countbranch;$i++){
			
			$data = "{ 
					y:" .$allbranchamt[$i].",
					color: colors[".$i."],
					drilldown: {
						name: '".$allbranchname[$i]."',
						categories: [".$categories_zone[$i]."],
						level: 1, 
						data: [".$outputdatalevel2[$i]."],
						color: colors[".$i."]
					}
				}";
			$outputdata .= $data;
			if($countbranch-$i>0){
				$outputdata .= ",";
			}
		}
		
		return $outputdata;
	}
	/*--------------------------------------------------------------------------------------------------------------------------------------------*/
?>
</head>